package com.bafa.front;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bafa.dbcache.LeagueCache;
import com.bafa.debug.Debug;
import com.bafa.exceptions.ErrorException;
import com.bafa.front.presentationbeans.LeagueBean;
import com.bafa.front.presentationbeans.LeaguePositionBean;
import com.bafa.front.presentationbeans.PlayerListBean;
import com.bafa.front.presentationbeans.TeamBean;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.PageBean;
import com.bafa.objects.team.Team;
import com.bafa.services.RetrieveServices;

public class DisplayTeamServlet extends DaddyServlet {

	
	@Override
	public void doEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {
		try{
		HttpSession session = request.getSession();
		LeagueBean league = (LeagueBean)session.getAttribute("DIVISION");
		TeamBean team = new TeamBean(RetrieveServices.getTeam(request.getParameter("TeamId")),FrontUtils.getThisSeason(request).getId());
		LeaguePositionBean allTimeRecord = new LeaguePositionBean(RetrieveServices.getTeam(request.getParameter("TeamId")).getId());
		LeaguePositionBean leagueRecord = new LeaguePositionBean(RetrieveServices.getTeam(request.getParameter("TeamId")).getId(),FrontUtils.getThisSeason(request).getId());
		PlayerListBean allTimeScorers = new PlayerListBean(RetrieveServices.getTeam(request.getParameter("TeamId")).getStats().scorers(),team.getId());
		request.setAttribute("SCORERS",allTimeScorers);
		request.setAttribute("RECORD",allTimeRecord);
		request.setAttribute("LEAGUERECORD",leagueRecord);
		request.setAttribute("TEAM",team);
		if(FrontUtils.isLoggedOnAsAdmin(request)){
			pageBean = (PageBean)LeagueCache.getPages().get("team");
			pageBean.setPageheader(team.getName());
		}else if(FrontUtils.isLoggedOn(request)){
			TeamBean sessionTeam = (TeamBean)session.getAttribute("LOGGEDONTEAM");
			if(sessionTeam!=null){
				if(team.getId() == sessionTeam.getId())
					url = "logon.bafa";
				else{
					Debug.debug(this.getClass(),"doEvent","Logged on but looking for another team.");
					pageBean = (PageBean)LeagueCache.getPages().get("team");
					Debug.debug(this.getClass(),"doEvent","Page is "+pageBean.getMainbody());
					pageBean.setPageheader(team.getName());
				}
			}
		} else{
			pageBean = (PageBean)LeagueCache.getPages().get("team");
			pageBean.setPageheader(team.getName());
			
			
		}
		request.setAttribute("Team",team);
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		
	
	}
}
